import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'

export const asyncThunk = createAsyncThunk('yyy', (test: string) => {
  return new Promise<string>((resolve) => {
    setTimeout(() => {
      resolve('123')
    }, 1000)
  })
})

export default createSlice({
  name: 'xxx',
  initialState: { lang: 'en', aaa: 123, bbb: 'abc', ccc: false },
  // 同步
  reducers: {
    setLang(state, action) {
      state.lang = action.payload
    },
  },
  // 异步
  extraReducers(builder) {
    builder.addCase(asyncThunk.fulfilled, (state: any, action) => {
      console.log(action)
      state.lang = action.meta.arg
    })
  },
})
